package com.qiaofang.collectmanage.service.common.utils.phone;

import com.alibaba.fastjson.JSONObject;
import com.qiaofang.collectmanage.constants.CollectRuleFieldEnum;
import com.qiaofang.collectmanage.constants.PersonalHouseSellEnum;
import com.qiaofang.collectmanage.service.common.bean.RequestResponseInfo;
import com.qiaofang.collectmanage.service.common.utils.HttpClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class RecognitionPhoneNumberUtils {
    private static final Logger logger = LoggerFactory.getLogger(RecognitionPhoneNumberUtils.class);

    public static String WUBA_PHONE_EVAL_URL = "https://apphouse.58.com/api/detail/hezu/{}?v=1&platform=android&format=json&sidDict=%7B%22PGTID%22%3A%22%22%2C%22GTID%22%3A%22%22%7D&localname=sh&signature=@@&version=7.15.3&commondata=%7B%22tracekey%22%3A%22aeea0d437cbc0da2ea98ae70542ebc95%22%2C%22hasNext%22%3Atrue%2C%22nextObserverIndex%22%3A0%7D";
    public static String PHONE_IMG_URL = "http://192.168.81.84:8000/getImg?imgUrl=";

    //识别手机号
    public static String recoginatePhonenumber(JSONObject jsonObject) {
        String phone = "";
        String link = (String) jsonObject.get(CollectRuleFieldEnum.REQUEST_DETAIL_URL.getValue());
        String mobileUrl = (String) jsonObject.get(PersonalHouseSellEnum.CONTACTMOBILE.getValue());
        //58同城扫毛电话号码识别
        if (link != null && link.contains("58.com")) {
            try {
                int start = link.lastIndexOf('/');
                int end = link.indexOf("x");
                if (start > 0 && end > start) {
                    String sub = link.substring(start + 1, end);
                    String signer = InfoIdSigneture.sign(sub);
                    String detailLink = WUBA_PHONE_EVAL_URL.replace("{}", sub).replace("@@", signer);
                    RequestResponseInfo requestResponseInfo = HttpClientUtils.getPagetext(detailLink, "utf-8");
                    String response = (requestResponseInfo != null) ? requestResponseInfo.getResponseText() : "";
                    if (StringUtils.isNotBlank(response) && response.contains("phonenum")) {
                        int s = response.lastIndexOf("phonenum") + 11;
                        int t = s + 32;
                        String encrpt = response.substring(s, t);
                        phone = AESCipher.evalPhone(encrpt);
                        jsonObject.put(PersonalHouseSellEnum.CONTACTMOBILE.getValue(), phone);
                    }
                }
            } catch (Exception e) {
                // TODO 钉钉报警
                logger.info(e.getMessage(), e);
            }
        } else if (link != null && link.contains("ganji.com")) {
            int start = link.lastIndexOf('/');
            int end = link.indexOf("x");
            if (start > 0 && end > start) {
                String sub = link.substring(start + 1, end);
                phone = GanjiHandler.getPhone(sub);
                jsonObject.put(PersonalHouseSellEnum.CONTACTMOBILE.getValue(), phone);
            }
            //手机号为图片的：房产超市
        } else if (mobileUrl != null && mobileUrl.contains("http")) {
            try {
                mobileUrl = mobileUrl.replace(" ", "");
                if (mobileUrl.charAt(mobileUrl.length() - 1) == ';') {
                    mobileUrl = mobileUrl.substring(0, mobileUrl.length() - 1);
                }
                String url = PHONE_IMG_URL + mobileUrl.replace("&", "@@");
                String str = HttpClientUtils.getStringByUrl(url, "utf-8");
                phone = str.replace(" ", "");
                jsonObject.put(PersonalHouseSellEnum.CONTACTMOBILE.getValue(), str);
            } catch (Exception e) {
                //TODO 钉钉报警手机号图片识别破解
                logger.info(e.getMessage(), e);
            }
        }
        return phone;
    }
}
